package com.confinderence;

import java.util.List;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.confinderence.model.Conference;
import com.confinderence.model.Lecture;

public class ConferenceDetailsFragment extends Fragment {
	public interface IProvider {
		public void initLecturesTable(TableLayout tl);
	}
	private TextView mStart;
	private TextView mEnd;
	private TableLayout mTableLayout;
	private Conference mConference;

	public ConferenceDetailsFragment(Context context, Conference conference) {
		mConference = conference;
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_conference_page, container, false);
		init(rootView);
		setTable();
		return rootView;
	}
	private void init(View rootView) {
		mTableLayout = (TableLayout) rootView.findViewById(R.id.conferenceLectureTable);
		mTableLayout.removeAllViews();
		TextView conferenceTitle = (TextView) rootView.findViewById(R.id.conferenceTitle);
		conferenceTitle.setText(mConference.getName());
		mStart = (TextView) rootView.findViewById(R.id.startLabel);
		mEnd = (TextView) rootView.findViewById(R.id.endLabel);
		String startLbl = getString(R.string.start_time);
		String endLbl = getString(R.string.end_time);
		mStart.setText(startLbl + " " + mConference.getStartTime().toString());
		mEnd.setText(endLbl + " " + mConference.getEndTime().toString());
		Button favouritesButton = (Button) rootView.findViewById(R.id.addToFavouritesButton);
		favouritesButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				addToFavourites();
			}
		});
		Button createLectureButton = (Button) rootView.findViewById(R.id.createLectureButton);
		createLectureButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				goToCreateLecture();
			}
		});
	}
	protected void addToFavourites() {
		Toast.makeText(getActivity(), getString(R.string.added_to_favourites), Toast.LENGTH_LONG)
				.show();
	}
	protected void goToCreateLecture() {
		Intent intent = new Intent(getActivity(), CreateLectureActivity.class);
		intent.putExtra(CreateLectureActivity.CONFERENCE, mConference);
		startActivity(intent);
	}
	private void setTable() {
		List<Lecture> lecturesList = mConference.getLectures();
		for (int i = 0; i < lecturesList.size(); i++) {
			final Lecture lecture = lecturesList.get(i);
			TableRow tr = new TableRow(getActivity());
			((ConferenceActivity) getActivity()).addCell(tr, lecture.getTitle(), 0.15f);
			((ConferenceActivity) getActivity()).addCell(tr, lecture.getDescription(), 0.15f);
			((ConferenceActivity) getActivity()).addCell(tr, lecture.getTime().toString(), 0.2f);
			((ConferenceActivity) getActivity()).addCell(tr, lecture.getDate().toString(), 0.3f);

			tr.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					goToLectureDetails(lecture);
				}
			});
			tr.setBackgroundColor(i % 2 == 0 ? 0xFF8C89D6 : 0xFFCECED6);
			mTableLayout.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT));
		}
		mTableLayout.setBackground(getResources().getDrawable(R.drawable.layout_border));
		mTableLayout.requestLayout();
	}
	protected void goToLectureDetails(Lecture lecture) {
		getFragmentManager().beginTransaction()
				.replace(R.id.container, new LectureDetailsFragment(lecture)).commit();
	}

}
